package com.ruoyi.web.controller.yang;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.yang.domain.ClassComment;
import com.ruoyi.yang.service.IClassCommentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yang.domain.TeacherClass;
import com.ruoyi.yang.service.ITeacherClassService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-04-09
 */
@RestController
@RequestMapping("/system/class")
public class TeacherClassController extends BaseController
{
    @Autowired
    private ITeacherClassService teacherClassService;
    @Autowired
    private IClassCommentService classCommentService;


    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:class:list')")
    @GetMapping("/list")
    public TableDataInfo list(TeacherClass teacherClass)
    {
        startPage();
        List<TeacherClass> list = teacherClassService.selectTeacherClassList(teacherClass);

        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:class:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TeacherClass teacherClass)
    {
        List<TeacherClass> list = teacherClassService.selectTeacherClassList(teacherClass);
        ExcelUtil<TeacherClass> util = new ExcelUtil<TeacherClass>(TeacherClass.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:class:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {

        return success(teacherClassService.selectTeacherClassById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:class:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TeacherClass teacherClass)
    {
        String username = SecurityUtils.getUsername();

        teacherClass.setTeaName(username);
        return toAjax(teacherClassService.insertTeacherClass(teacherClass));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:class:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody TeacherClass teacherClass)
    {
        String username = SecurityUtils.getUsername();
        if (teacherClass.getTeaName().equals(username)){
            Integer a =teacherClassService.updateTeacherClass(teacherClass);
            System.out.println(a);
            return toAjaxTea(a);
        }else {
            return toAjaxTea(0);
        }

    }
    /**
     * 选课
     */
    @Transactional
    @PreAuthorize("@ss.hasPermi('system:class:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping("/checkClass")
    public AjaxResult CheckClass(@RequestBody TeacherClass teacherClass)
    {
        Long classid = teacherClass.getId();
        Long userid = SecurityUtils.getUserId();
        if (classCommentService.selectOne(userid,classid).isEmpty()){
            String username = SecurityUtils.getUsername();
            ClassComment classComment = new ClassComment();
            //课程id的录入
            classComment.setClassId(teacherClass.getId());
            //课程名的录入
            classComment.setClassName(teacherClass.getCurriculum());
            //课程类型
            classComment.setClassType(teacherClass.getTeaType());
            //教师名称的录入
            classComment.setClassTea(teacherClass.getTeaName());
            //选课人id的录入
            classComment.setStuId(userid);
            //选选课人姓名的录入
            classComment.setStuName(username);
            //插入语句,插入教师表=============================>>>>
            classCommentService.insertClassComment(classComment);
            teacherClass.setNumber(teacherClass.getNumber()+1);
            Integer a =teacherClassService.checkClass(teacherClass);
            return toAjaxCla(a);
        }
        else {
            return toAjaxCla(0);
        }


    }







    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:class:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids,String name)
    {
        if (name.equals(SecurityUtils.getUsername())){
            return toAjax(teacherClassService.deleteTeacherClassByIds(ids));
        }else {
            return toAjaxTea(0);
        }

    }
}
